/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.match.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.io.FileInputStream;
import java.io.IOException;


import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JDialog;

/**
 * @author Massimo Canonaco
 *
 */
public class TurnDialog extends JDialog 
{
	/** */
	static private final int STARTING_COUNTER_VALUE = 16;
	
	/** */
	private Image background;
	
	/** */
	private Image turnOnBackground;
	
	/** */
	private Image turnOffBackground;
	
	/** */
	private int counter;
	
	/** */
	private boolean show;
	/** */
	public TurnDialog(Frame mainFrame, Map<String,String> settings)
	{
		super(mainFrame);
		show = false;
		System.out.println("RICREO TURNDIALOG");
		try {
			turnOnBackground = ImageIO.read(new FileInputStream(settings.get("turnpanel.on.background.path")));
			turnOffBackground = ImageIO.read(new FileInputStream(settings.get("turnpanel.off.background.path")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		counter = STARTING_COUNTER_VALUE;
		
		setMinimumSize(new Dimension(turnOnBackground.getWidth(null),turnOffBackground.getHeight(null)));
		setFocusable(false);
		setUndecorated(true);
		setLocation(mainFrame.getWidth()/2 - turnOnBackground.getWidth(null)/2, mainFrame.getHeight()/2 - turnOffBackground.getHeight(null)/2);
		getRootPane().setOpaque(false);
		setBackground(new Color(0,0,0,0));
		setBackground(true);
		setAlwaysOnTop(false);
		setModal(false);
//		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		pack();
		
	}
	
	/** */
	public void setBackground(boolean turnOn)
	{
		if(turnOn)
		{
			background = turnOnBackground;
		}
		else
		{
			background = turnOffBackground;
		}
	}
	
	/** */
	public void update()
	{
		if(show)
		{
			if(counter <=0 )
			{
				setVisible(false);
				show = false;
			}
			else
			{
				--counter;
			}
			
		}
//		System.out.println("update in turn dialog "+counter);
	}
	
	@Override
	public void setVisible(boolean b) 
	{
		System.out.println("IMPOSTO VISIBLE "+b);
		
		if(b)
		{
			counter = STARTING_COUNTER_VALUE;
			show = true;
		}
		super.setVisible(b);
	}
	
		@Override
		public void paint(Graphics g) 
		{
			System.out.println("painting "+isVisible());
			super.paint(g);
			g.drawImage(background, 0, 0, null);
		}
//	@Override
//	public void paintComponents(Graphics g) 
//	{
//		super.paintComponents(g);
//		g.drawImage(background, 0, 0, null);
//	}
}
